//
//  MHCommentVCViewController.m
//  ManHua
//
//  Created by lanou on 15/11/4.
//  Copyright © 2015年 蓝欧科技. All rights reserved.
//

#import "MHCommentVCViewController.h"
#import "MHComicComentModel.h"
#import "MHComicCommentTableViewCell.h"
#import "MHDataManager.h"
#import "MHUserInfoSington.h"
#import <AFNetworking.h>
#import "MJRefresh.h"
@interface MHCommentVCViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataSourceArr;
@property(nonatomic,strong)NSString *pageIndex;
@property(nonatomic,assign)NSInteger commentNumber;
@property(nonatomic,assign)NSInteger userID;
@end

@implementation MHCommentVCViewController
-(NSMutableArray *)dataSourceArr
{
    if (_dataSourceArr==nil) {
        self.dataSourceArr=[NSMutableArray arrayWithCapacity:0];
    }
    return _dataSourceArr;
}
-(void)acquireComemetNumber
{
    NSString *filePathString=[NSString stringWithFormat:@"http://112.124.96.190:9090/bukamanhua5appjobs/ComicHandle.ashx?method=usercommentcount&userid=%ld",self.userID];
    NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:filePathString] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        if (connectionError!=nil) {
            NSLog(@"请求数据失败%@",connectionError);
        }
        else
        {
            NSString *result=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"%@",result);
            self.commentNumber=[result integerValue];
            
            
        }
    }];
}
-(void)acquireCommentData
{
    
//
    
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    if ([user boolForKey:@"isLogin"]) {
        [self acquireComemetNumber];
 self.userID=  [MHUserInfoSington sharedInstance].userInfo.UserID;
    NSMutableDictionary *Dict=[NSMutableDictionary   dictionary];
    Dict[@"method"]=@"usercommentlist";
    Dict[@"userid"]=[NSString stringWithFormat:@"%ld",self.userID];
    Dict[@"pageindex"]=self.pageIndex;
    Dict[@"uuid"]=@"537e76184314b8c8beb37f75bacafb14edbe0d97";
        if ([self.pageIndex isEqualToString:@"0"]) {
            [self.dataSourceArr removeAllObjects];
        }
    [[AFHTTPRequestOperationManager manager]GET:@"http://112.124.96.190:9090/bukamanhua5appjobs/ComicHandle.ashx" parameters:Dict success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        [responseObject enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            MHComicComentModel *commentModel=[[MHComicComentModel alloc]init];
            [commentModel setValuesForKeysWithDictionary:obj];
        
            [self.dataSourceArr addObject:commentModel];
            
           
        }];
        
         [self.tableView reloadData];
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        
        NSLog(@"解析失败%@",error.localizedDescription);
    }];
    }

}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   self.pageIndex=@"0";
    [self acquireCommentData];
    self.title = @"我的评论";
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, Width, Height-70)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    __weak MHCommentVCViewController *test=self;
    _tableView.header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self acquireCommentData];
    }];
    _tableView.footer=[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        NSInteger pageNumber=[self.pageIndex integerValue];
        self.pageIndex=[NSString stringWithFormat:@"%ld",++pageNumber];
        if (test.dataSourceArr.count >=self.commentNumber) {
            [self.tableView.footer endRefreshingWithNoMoreData];
        }
        else
        {
        [self acquireComemetNumber];
        }
    }];
    [_tableView registerClass:[MHComicCommentTableViewCell class] forCellReuseIdentifier:@"MHComicCommentTableViewCell"];
    [self.view addSubview:_tableView];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MHComicComentModel *model=self.dataSourceArr[indexPath.row];
    return [MHComicCommentTableViewCell heightOfCellWithModdel:model];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MHComicCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MHComicCommentTableViewCell" forIndexPath:indexPath];
    MHComicComentModel *commentModel = self.dataSourceArr[indexPath.row];
    [cell configureDataWithModel:commentModel];
    return cell;

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
